<template>
  <div class="app-container">
    <div
      style="
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin: 0px 20px 10px 0px;
      "
    >
      <el-button
        type="primary"
        plain
        icon="Plus"
        @click="handleAdd"
        v-hasPermi="['signIn:configuration:add']"
        style="margin-left: 20px"
      >
        新增签到配置
      </el-button>

      <el-input
        v-model="queryParams.visitObject"
        style="max-width: 200px; margin-left: 10px"
        placeholder="请输入访问对象"
        @change="handleQuery"
      >
        <template #append>
          <el-button :icon="Search" />
        </template>
      </el-input>
    </div>
    <el-table
      v-loading="loading"
      :data="postList"
      :default-sort="{ prop: 'postId', order: 'descending' }"
    >
      <el-table-column align="center" prop="activityName" label="名称" />
      <el-table-column align="center" prop="visitObject" label="拜访对象" />
      <el-table-column align="center" prop="startTime" label="签到开始时间" />
      <el-table-column align="center" prop="endTime" label="签到结束时间" />
      <el-table-column align="center" prop="addressName" label="地点" />
      <el-table-column align="center" prop="addressNameDetail" label="经纬度" />
      <el-table-column align="center" prop="attendIds" label="参加人员">
        <template #default="scope">
          <span v-for="item in scope.row.attendIds" :key="item">
            {{ "  " }}
            {{ userList.filter((v) => v.userId == item)[0].userName }}{{ "  " }}
          </span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="是否必须备注">
        <template #default="scope">
          <span v-if="scope.row.isRemark == 0">否</span>
          <span v-if="scope.row.isRemark == 1">是</span>
          <span
            v-if="scope.row.isRemark != 0 && scope.row.isRemark != 1"
          ></span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="是否必须拍照">
        <template #default="scope">
          <span v-if="scope.row.isPhoto == 0">否</span>
          <span v-if="scope.row.isPhoto == 1">是</span>
          <span v-if="scope.row.isPhoto != 0 && scope.row.isPhoto != 1"></span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="创建人">
        <template #default="scope">
          <span>{{
            userList.filter((item) => item.userId == scope.row.createId)[0]
              .userName
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="180">
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['signIn:configuration:edit']"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['signIn:configuration:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 修改签到记录对话框 -->
    <el-dialog :title="title" v-model="open" width="700px" append-to-body>
      <el-form ref="postRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="名称" prop="activityName">
          <el-input v-model="form.activityName" placeholder="名称" />
        </el-form-item>

        <el-form-item label="拜访对象" prop="visitObject">
          <el-input v-model="form.visitObject" placeholder="拜访对象" />
        </el-form-item>

        <el-form-item label="签到开始时间" prop="startTime">
          <el-date-picker
            v-model="form.startTime"
            type="datetime"
            placeholder="请输入签到开始时间"
            value-format="YYYY-MM-DD HH:mm:ss"
          />
        </el-form-item>

        <el-form-item label="签到结束时间" prop="endTime">
          <el-date-picker
            v-model="form.endTime"
            type="datetime"
            placeholder="请输入签到结束时间"
            value-format="YYYY-MM-DD HH:mm:ss"
          />
        </el-form-item>

        <el-form-item label="地点名称" prop="addressName">
          <el-input v-model="form.addressName" placeholder="地点名称" />
        </el-form-item>

        <el-form-item label="经纬度" prop="addressNameDetail">
          <div style="display: flex">
            <el-input
              v-model="form.addressNameDetail"
              placeholder="经纬度 例如：104.061382,30.569197"
            />
            <el-button type="text">地图定位</el-button>
          </div>
        </el-form-item>

        <el-form-item label="人员" prop="attendIds">
          <el-select
            v-model="form.attendIds"
            placeholder="请选择人员"
            multiple
            filterable
          >
            <el-option
              :label="item.userName"
              :value="item.userId"
              v-for="item in userList"
              :key="item.userId"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="是否必须备注" prop="isRemark">
          <el-radio-group v-model="form.isRemark">
            <el-radio :value="0">否</el-radio>
            <el-radio :value="1">是</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="是否必须拍照" prop="isPhoto">
          <el-radio-group v-model="form.isPhoto">
            <el-radio :value="0">否</el-radio>
            <el-radio :value="1">是</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script setup>
import { Search } from "@element-plus/icons-vue";
import { ref } from "vue";
import {
  getSignConfigApi,
  addSignConfigApi,
  deleteSignConfigApi,
  getSignConfigByIdApi,
  putSignConfigApi,
  getAlluserListApi,
} from "@/api/sign/configuration";

const { proxy } = getCurrentInstance();
const postList = ref([]);
const loading = ref(true);
const total = ref(0);
const open = ref(false);
const title = ref("");
const data = reactive({
  form: {
    activityName: "",
    visitObject: "",
    startTime: "",
    endTime: "",
    addressName: "",
    addressNameDetail: "",
    attendIds: [],
    isRemark: "",
    isPhoto: "",
  },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    visitObject: "",
  },
  rules: {
    activityName: [
      { required: true, message: "名称不能为空", trigger: "blur" },
    ],
    addressNameDetail: [
      { required: false, message: "请输入经纬度", trigger: "blur" },
      {
        pattern:
          /^[\-\+]?(0?\d{1,2}\.\d{1,6}|1[0-7]?\d{1}\.\d{1,6}|180\.0{1,6}),[\-\+]?([0-8]?\d{1}\.\d{1,6}|90\.0{1,6})/,
        message: "经纬度格式不正确，正确格式为：104.061382,30.569197",
        trigger: "blur",
      },
    ],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询签到记录列表 */
function getList() {
  loading.value = true;
  getSignConfigApi(queryParams.value).then((response) => {
    postList.value = response.rows;
    total.value = Number(response.total);
    loading.value = false;
  });
}

/**
 * 按名字搜索签到配置
 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/**
 * 重置
 */
const rest = () => {
  form.value = {
    activityName: "",
    visitObject: "",
    startTime: "",
    endTime: "",
    addressName: "",
    addressNameDetail: "",
    attendIds: [],
    isRemark: "",
    isPhoto: "",
  };
  proxy.resetForm("postRef");
};

const userList = ref([]);
/**
 * 获取人员列表
 */
const getAlluserList = () => {
  getAlluserListApi().then((res) => {
    userList.value = res;
  });
};
getAlluserList();

/** 新增按钮操作 */
function handleAdd() {
  rest();
  open.value = true;
  title.value = "新增广告配置";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  rest();
  const postId = row.id;
  getSignConfigByIdApi(postId).then((response) => {
    form.value = {
      id: response.data.id,
      activityName: response.data.activityName,
      visitObject: response.data.visitObject,
      startTime: response.data.startTime,
      endTime: response.data.endTime,
      addressName: response.data.addressName,
      addressNameDetail: response.data.addressNameDetail,
      attendIds: response.data.attendIds.map((item) => {
        return (item = String(item));
      }),
      isRemark: response.data.isRemark,
      isPhoto: response.data.isPhoto,
    };
    open.value = true;
    title.value = "修改广告配置";
  });
}

/** 取消按钮 */
function cancel() {
  rest();
  open.value = false;
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["postRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != undefined) {
        putSignConfigApi(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addSignConfigApi(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const postIds = row.id;
  proxy.$modal
    .confirm(`是否确认删除名称为${row.activityName}的签到配置数据项？`)
    .then(function () {
      return deleteSignConfigApi(postIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

getList();
</script>
<style scoped>
:deep(.el-form-item) {
  display: flex;
  justify-content: flex-start;
  align-items: center;
}
</style>
